<template>
  <el-dialog
    :title="!dataForm.orderid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品名称" prop="goodsName">
      <el-input v-model="dataForm.goodsName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="商品id" prop="goodsId">
      <el-input v-model="dataForm.goodsId" placeholder="商品id"></el-input>
    </el-form-item>
    <el-form-item label="商品价格" prop="goodsPrice">
      <el-input v-model="dataForm.goodsPrice" placeholder="商品价格"></el-input>
    </el-form-item>
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="用户姓名" prop="username">
      <el-input v-model="dataForm.username" placeholder="用户姓名"></el-input>
    </el-form-item>
    <el-form-item label="直播活动id" prop="liveId">
      <el-input v-model="dataForm.liveId" placeholder="直播活动id"></el-input>
    </el-form-item>
    <el-form-item label="直播活动标题" prop="liveTittle">
      <el-input v-model="dataForm.liveTittle" placeholder="直播活动标题"></el-input>
    </el-form-item>
    <el-form-item label="直播活动图片" prop="liveImg">
      <el-input v-model="dataForm.liveImg" placeholder="直播活动图片"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createtim">
      <el-input v-model="dataForm.createtim" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="订单状态" prop="payStatus">
      <el-input v-model="dataForm.payStatus" placeholder="订单状态"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          orderid: 0,
          goodsName: '',
          goodsId: '',
          goodsPrice: '',
          userId: '',
          username: '',
          liveId: '',
          liveTittle: '',
          liveImg: '',
          createtim: '',
          payStatus: ''
        },
        dataRule: {
          goodsName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          goodsId: [
            { required: true, message: '商品id不能为空', trigger: 'blur' }
          ],
          goodsPrice: [
            { required: true, message: '商品价格不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          username: [
            { required: true, message: '用户姓名不能为空', trigger: 'blur' }
          ],
          liveId: [
            { required: true, message: '直播活动id不能为空', trigger: 'blur' }
          ],
          liveTittle: [
            { required: true, message: '直播活动标题不能为空', trigger: 'blur' }
          ],
          liveImg: [
            { required: true, message: '直播活动图片不能为空', trigger: 'blur' }
          ],
          createtim: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          payStatus: [
            { required: true, message: '订单状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.orderid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.orderid) {
            this.$http({
              url: this.$http.adornUrl(`/live/liveorder/info/${this.dataForm.orderid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.goodsName = data.liveOrder.goodsName
                this.dataForm.goodsId = data.liveOrder.goodsId
                this.dataForm.goodsPrice = data.liveOrder.goodsPrice
                this.dataForm.userId = data.liveOrder.userId
                this.dataForm.username = data.liveOrder.username
                this.dataForm.liveId = data.liveOrder.liveId
                this.dataForm.liveTittle = data.liveOrder.liveTittle
                this.dataForm.liveImg = data.liveOrder.liveImg
                this.dataForm.createtim = data.liveOrder.createtim
                this.dataForm.payStatus = data.liveOrder.payStatus
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/live/liveorder/${!this.dataForm.orderid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'orderid': this.dataForm.orderid || undefined,
                'goodsName': this.dataForm.goodsName,
                'goodsId': this.dataForm.goodsId,
                'goodsPrice': this.dataForm.goodsPrice,
                'userId': this.dataForm.userId,
                'username': this.dataForm.username,
                'liveId': this.dataForm.liveId,
                'liveTittle': this.dataForm.liveTittle,
                'liveImg': this.dataForm.liveImg,
                'createtim': this.dataForm.createtim,
                'payStatus': this.dataForm.payStatus
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
